﻿using System.Collections.Generic;
using System.Threading.Tasks;
using ERP.ERPSystem.ProductionSystem;
using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Common.DTO;
using Microsoft.AspNetCore.Mvc;

namespace ERP.Web.Controllers.ProductionSystem
{
    /// <summary>
    /// 供应商管理控制器
    /// </summary>
    [Route("[controller]/[action]")]
    public class SupplierController : Controller
    {
        /// <summary>
        /// 供应商服务接口
        /// </summary>
        private readonly ISupplierServer _supplierServer;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="supplierServer">供应商服务接口</param>
        public SupplierController(ISupplierServer supplierServer)
        {
            _supplierServer = supplierServer;
        }

        /// <summary>
        /// 供应商管理首页视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult SupplierIndex()
        {
            return View();
        }



        // 分页查询
        [Route("GetSupplierPagedListAsync")]
        [HttpGet]
        public async Task<JsonResult> GetSupplierPagedListAsync(string keyword = null, bool? isEnabled = null, int page = 1, int pageSize = 10)
        {
            var result = await _supplierServer.GetPagedListAsync(keyword, isEnabled, page, pageSize);
            return Json(result);
        }

        // 获取详情
        [Route("GetSupplierByIdAsync")]
        [HttpGet]
        public async Task<JsonResult> GetSupplierByIdAsync(int id)
        {
            var result = await _supplierServer.GetByIdAsync(id);
            return Json(result);
        }

        // 新增/编辑
        [Route("SaveSupplierAsync")]
        [HttpPost]
        public async Task<JsonResult> SaveSupplierAsync([FromBody] SupplierDTO entity)
        {
            var result = await _supplierServer.SaveAsync(entity);
            return Json(result);
        }

        // 删除
        [Route("DeleteSupplierAsync")]
        [HttpPost]
        public async Task<JsonResult> DeleteSupplierAsync(int id)
        {
            var result = await _supplierServer.DeleteAsync(id);
            return Json(result);
        }

        // 批量删除
        [Route("DeleteSupplierBatchAsync")]
        [HttpPost]
        public async Task<JsonResult> DeleteSupplierBatchAsync([FromBody] List<int> ids)
        {
            var result = await _supplierServer.DeleteBatchAsync(ids);
            return Json(result);
        }
    }
}
